package stardust.core.network.protocol.socks4;


import java.io.IOException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import stardust.core.network.NetworkException;
import stardust.core.network.NetworkSessionInputStream;


public class Socks4aRequestMeta
{

  private SocksVersion version;

  private SocksCommand command;

  private int port;

  private String host;

  private String userIdentity;

  public Socks4aRequestMeta()
  {
  }

  public static Socks4aRequestMeta getRequest(NetworkSessionInputStream reader) throws IOException
  {
    Socks4aRequestMeta meta = new Socks4aRequestMeta();

    meta.version = SocksVersion.get(reader.read());
    meta.command = SocksCommand.get(reader.read());

    byte[] buff = new byte[2];

    buff[0] = (byte) reader.read();
    buff[1] = (byte) reader.read();
    meta.port = new BigInteger(buff).byteValue();

    buff[0] = (byte) reader.read();
    buff[1] = (byte) reader.read();
    meta.host = String.valueOf(new BigInteger(buff).byteValue());
    buff[0] = (byte) reader.read();
    buff[1] = (byte) reader.read();
    meta.host += String.valueOf(new BigInteger(buff).byteValue());
    buff[0] = (byte) reader.read();
    buff[1] = (byte) reader.read();
    meta.host += String.valueOf(new BigInteger(buff).byteValue());
    buff[0] = (byte) reader.read();
    buff[1] = (byte) reader.read();
    meta.host += String.valueOf(new BigInteger(buff).byteValue());

    ByteBuffer byteBuff = ByteBuffer.allocate(32);
    int i;

    while ((i = reader.read()) != -1)
    {
      if (i == 0)
      {
        meta.userIdentity = new String(byteBuff.array());
      }
      else
      {
        byteBuff.put((byte) i);
      }
    }

    throw new NetworkException("异常的 Socks4 请求格式。");
  }

  public static Socks4aRequestMeta parseRequest(NetworkSessionInputStream reader) throws IOException
  {
    Socks4aRequestMeta meta = new Socks4aRequestMeta();

    meta.version = SocksVersion.get(reader.read());
    meta.command = SocksCommand.get(reader.read());

    byte[] buff = new byte[2];

    buff[0] = (byte) reader.read();
    buff[1] = (byte) reader.read();
    meta.port = new BigInteger(buff).byteValue();

    buff[0] = (byte) reader.read();
    buff[1] = (byte) reader.read();
    meta.host = String.valueOf(new BigInteger(buff).byteValue());
    buff[0] = (byte) reader.read();
    buff[1] = (byte) reader.read();
    meta.host += String.valueOf(new BigInteger(buff).byteValue());
    buff[0] = (byte) reader.read();
    buff[1] = (byte) reader.read();
    meta.host += String.valueOf(new BigInteger(buff).byteValue());
    buff[0] = (byte) reader.read();
    buff[1] = (byte) reader.read();
    meta.host += String.valueOf(new BigInteger(buff).byteValue());

    ByteBuffer byteBuff = ByteBuffer.allocate(32);
    int i;

    while ((i = reader.read()) != -1)
    {
      if (i == 0)
      {
        meta.userIdentity = new String(byteBuff.array());
      }
      else
      {
        byteBuff.put((byte) i);
      }
    }

    throw new NetworkException("异常的 Socks4 请求格式。");
  }

}
